package com.sayimo.maker.mall.goods.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sayimo.framework.common.AbstractBaseService;
import com.sayimo.framework.exception.DaoException;
import com.sayimo.framework.exception.ServiceException;
import com.sayimo.maker.activity.dao.BaseActivityDao;
import com.sayimo.maker.activity.dao.BaseCutDao;
import com.sayimo.maker.activity.dao.BaseGroupBuyDao;
import com.sayimo.maker.activity.dao.BaseSeckillDao;
import com.sayimo.maker.activity.dao.BaseSeckillGoodsDao;
import com.sayimo.maker.activity.dao.BaseSeckillTimesDao;
import com.sayimo.maker.activity.vo.BaseCut;
import com.sayimo.maker.commons.Constant;
import com.sayimo.maker.mall.goods.dao.GoodsListInterfaceDao;
import com.sayimo.maker.mall.goods.dao.GoodsNormsInfoInterfaceDao;
import com.sayimo.maker.mall.goods.service.GoodsNormsInfoInterfaceSerice;
import com.sayimo.maker.mall.goods.vo.GoodsNormsInfo;
import com.sayimo.maker.mall.orders.dao.OrdersListInterfaceDao;



@Service(value = "goodsNormsInfoInterfaceSerice")
public class GoodsNormsInfoInterfaceImplSerice extends AbstractBaseService implements GoodsNormsInfoInterfaceSerice {

	@Resource(name = "goodsNormsInfoInterfaceDao")
	private GoodsNormsInfoInterfaceDao goodsNormsInfoInterfaceDao;
	@Autowired
	private BaseActivityDao baseActivityDao;
	@Autowired
	private BaseSeckillDao baseSeckillDao;
	@Autowired
	private BaseGroupBuyDao baseGroupBuyDao;
	@Autowired
	private BaseSeckillTimesDao baseSeckillTimesDao;
	@Autowired
	private BaseSeckillGoodsDao baseSeckillGoodsDao;
	@Autowired
	private GoodsListInterfaceDao goodsListInterfaceDao;
	@Autowired
	private OrdersListInterfaceDao ordersListInterfaceDao;
	@Autowired
	private BaseCutDao baseCutDao;


	@Override
	public GoodsNormsInfo queryGoodsNormsByGoodsIdNormsValuesId(Integer goodsId, Integer goodsNormsValueId) throws ServiceException {
		try {
			Map<String, Object> pm = new HashMap<String, Object>();
			pm.put("goodsId", goodsId);
			pm.put("goodsNormsValueId", goodsNormsValueId);
			return goodsNormsInfoInterfaceDao.queryGoodsNormsByGoodsIdNormsValuesId(pm);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}

	
	/**
	 * 
	* @Title: allNormsValues 
	* @Description: 根据商品ID 查询 所有规格组合
	* @param goodsId
	* @return
	* @throws ServiceException
	* @return Map
	* @throws
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Map allNormsValues(Integer goodsId)
			throws ServiceException {
		Map map=new HashMap();
		try{
			List<String> idList=goodsNormsInfoInterfaceDao.getAllNormsValuesIdByGoodsId(goodsId);
			if(idList!=null){
				if(idList.size()>0){
					List<String> list1=new ArrayList<String>();
					List<String> list2=new ArrayList<String>();
					for(String str:idList){
						String[] strings=str.split(",");
						if(!"0".equals(strings[0])){
							list1.add(strings[0]);
						}
						if(!"0".equals(strings[1])){
							list2.add(strings[1]);
						}
					}
					List<String> list3=new ArrayList<String>();
					List<String> list4=new ArrayList<String>();
					for(String str:list1){
						if(!list3.contains(str)){
							list3.add(str);
						}
					}
					for(String str:list2){
						if(!list4.contains(str)){
							list4.add(str);
						}
					}
					Map map1=new HashMap();
					if(list3.size()>0){
						String name1=goodsNormsInfoInterfaceDao.getNormsNameByNormsValueId(Integer.valueOf(list3.get(0)));
						List<Map> normsValuesList1=new ArrayList<Map>();
						for(String str:list3){
						 	Map temp1=goodsNormsInfoInterfaceDao.getNormsValuesInfoById(Integer.valueOf(str));
						 	normsValuesList1.add(temp1);
						}
						
						map1.put("normsName", name1);
						map1.put("normsValuesList", normsValuesList1);
					}
					
					Map map2=new HashMap();
					if(list4.size()>0){
						String name2=goodsNormsInfoInterfaceDao.getNormsNameByNormsValueId(Integer.valueOf(list4.get(0)));
						List<Map> normsValuesList2=new ArrayList<Map>();
						for(String str:list4){
						 	Map temp2=goodsNormsInfoInterfaceDao.getNormsValuesInfoById(Integer.valueOf(str));
						 	normsValuesList2.add(temp2);
						}
						
						map2.put("normsName", name2);
						map2.put("normsValuesList", normsValuesList2);
					}
					
					
					
					
					
					List<Map> resultList=new ArrayList<Map>();
					if(list3.size()>0){
						resultList.add(map1);
					}
					if(list4.size()>0){
						resultList.add(map2);
					}
					Map resultMap=new HashMap();
					resultMap.put("normList", resultList);
					
					map.put(Constant.STATUS, Constant.STATUS_SUCCESS);
					map.put(Constant.DATA,resultMap);
					
				}else{
					map.put(Constant.STATUS, Constant.STATUS_ERROR);
					map.put(Constant.ERROR_CODE, Constant.SYSTEM_NO_DATA);
					return map;
				}
			}else{
				map.put(Constant.STATUS, Constant.STATUS_ERROR);
				map.put(Constant.ERROR_CODE, Constant.SELECT_ERROR);
				return map;
			}
			
		}catch(DaoException de){
			throw new ServiceException(de);
		}
		return map;
	}
	
	
	/**
	 * 
	* @Title: normsInfo 
	* @Description: 查询规格信息
	* @param goodsId
	* @param normsValueIds
	* @return
	* @throws ServiceException
	* @return Map
	* @throws
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Map normsInfo(Integer goodsId, String normsValueIds,String isActivity, String identifier,Integer seckillTimesId,Integer customerId)
			throws ServiceException {
		Map map=new HashMap();
		try{
			Map temp=goodsNormsInfoInterfaceDao.getNormsInfo(goodsId, normsValueIds);
			if(temp!=null){
				if(isActivity != null && !isActivity.equals("") && isActivity.equals("1") ){
					//如果为秒抢活动
					if ( identifier != null	&& !identifier.equals("") && (identifier.contains("PT_SPMQ"))) {
						//获取秒抢活动信息
						Map<String,Object> actMap = this.baseSeckillDao.selectBaseSeckillIdentifier(identifier);
						//秒抢活动限购
						Integer goodsBuy = (Integer) actMap.get("goodsRestrictions");
						
						Integer customerGoodsBuy =0;
						//获取会员当天在该活动的当前时间段购买商品总数
						customerGoodsBuy = ordersListInterfaceDao.getCustomerSeckillGoodsBuy(customerId,identifier,seckillTimesId);
						
						
						//获取秒抢活动商品信息
						Map<String,Object> activityGoodsMap = this.baseSeckillGoodsDao.querySeckillGoodsInfoByGoodsId(goodsId,identifier,seckillTimesId);
						//如果活动价有%为按价格打折，否则为活动价格
						if(activityGoodsMap.get("preferentialPrice").toString().indexOf("%")>=0){
							Double activityPrice=Double.parseDouble(activityGoodsMap.get("preferentialPrice").toString().replaceAll("%", ""));
							Double preferentialPrice=Double.parseDouble(temp.get("preferentialPrice").toString());
							
							temp.put("preferentialPrice", new java.text.DecimalFormat("#.00").format(activityPrice*preferentialPrice/100.00));
						}else{
							temp.put("preferentialPrice", Double.parseDouble(activityGoodsMap.get("preferentialPrice").toString()));
						}
						//活动库存
						Integer stock = (Integer)  activityGoodsMap.get("stock");
						
						//判断库存
						if(stock>0){
							if(goodsBuy-customerGoodsBuy<=0){
								temp.put("stock", 0);
							}else{
								if(stock>goodsBuy-customerGoodsBuy){
									temp.put("stock", goodsBuy-customerGoodsBuy);
								}else{
									temp.put("stock", stock);
								}
								
							}
							
						}else{
							temp.put("stock", 0);
						}
						
					}else if(identifier != null	&& !identifier.equals("") && (identifier.contains("PT_TG_"))){   //如果为团购活动
						//获取团购活动信息
						Map<String,Object> actMap = this.baseGroupBuyDao.selectBaseGroupBuyIdentifier(identifier);
						//团购活动限购
						Integer goodsBuy = (Integer) actMap.get("maxBuy");
						
						Integer customerGoodsBuy =0;
						//获取会员该活动的购买商品总数
						customerGoodsBuy = ordersListInterfaceDao.getCustomerGroupGoodsBuy(customerId,identifier);
						
						

						//如果活动价有%为按价格打折，否则为活动价格
						if(actMap.get("preferentialPrice").toString().indexOf("%")>=0){
							Double activityPrice=Double.parseDouble(actMap.get("preferentialPrice").toString().replaceAll("%", ""));
							Double preferentialPrice=Double.parseDouble(temp.get("preferentialPrice").toString());
						
							temp.put("preferentialPrice", new java.text.DecimalFormat("#.00").format(activityPrice*preferentialPrice/100.00));
						}else{
							temp.put("preferentialPrice", Double.parseDouble(actMap.get("preferentialPrice").toString()));

						}
						//活动库存
						Integer stock = (Integer)  actMap.get("stock");
						
						//判断库存
						if(goodsBuy == 0){  //0表示不限购
							temp.put("stock", stock);
						}else{  //goodsBuy:限购
							if(stock>0){
								if(goodsBuy-customerGoodsBuy<=0){
									temp.put("stock", 0);
								}else{
									if(stock>goodsBuy-customerGoodsBuy){
										temp.put("stock", goodsBuy-customerGoodsBuy);
									}else{
										temp.put("stock", stock);
									}
								}
							}else{
								temp.put("stock", 0);
							}	
						}
						temp.put("number",actMap.get("number"));
					}else if ( identifier != null	&& !identifier.equals("") && (identifier.contains("PT_KJ"))) {
						//获取砍价活动信息
						Map cutMap=new HashMap();
						cutMap.put("identifier", identifier);
						List<BaseCut> CutList=new ArrayList<BaseCut>();
						CutList=baseCutDao.selectBaseCutByIdentifier(cutMap);

						Integer customerGoodsBuy =0;
						//获取会员在该砍价活动购买商品总数
						customerGoodsBuy = ordersListInterfaceDao.getCustomerCutPriceGoodsBuy(customerId,identifier);
					
						//活动价格
						temp.put("preferentialPrice", Double.parseDouble(CutList.get(0).getPreferentialPrice().toString()));
						//活动商品名称
						temp.put("goodsName", CutList.get(0).getGoodsName());
						//活动库存
						Integer stock = CutList.get(0).getNumber();

						//判断库存
						if(stock>0){
							temp.put("stock", stock);
						}else{
							temp.put("stock", 0);
						}
						
					}else{
						//如果为预定预约活动
						//获取活动信息
						Map actMap = this.baseActivityDao.selectBaseActivityIdentifier(identifier);
						//限购类型，0 所有商品限购；1 单个商品限购； 2 每日所有商品限购
						Integer limitType = (Integer) actMap.get("limitType");
						//活动限购
						Integer goodsBuy = (Integer) actMap.get("goodsBuy");
						
						Integer customerGoodsBuy =0;
						if(limitType==1){
							//获取会员购买该活动中该单个商品的购买数量
							customerGoodsBuy = ordersListInterfaceDao.getCustomerGoodsBuyByOrder(customerId,goodsId,identifier,limitType);
						}else if(limitType==0){
							//获取会员在该活动的购买商品总数
							customerGoodsBuy = ordersListInterfaceDao.getCustomerGoodsBuyByOrder(customerId,null,identifier,limitType);
						}else if(limitType==2){
							//获取会员当天在该活动的购买商品总数
							customerGoodsBuy = ordersListInterfaceDao.getCustomerGoodsBuyByOrder(customerId,null,identifier,limitType);
						}
						
						//获取活动商品信息
						Map<String,Object> activityGoodsMap = goodsListInterfaceDao.queryActivityGoodsInfoByGoodsId(goodsId,identifier);
						//如果活动价有%为按价格打折，否则为活动价格
						if(activityGoodsMap.get("preferentialPrice").toString().indexOf("%")>=0){
							Double activityPrice=Double.parseDouble(activityGoodsMap.get("preferentialPrice").toString().replaceAll("%", ""));
							Double preferentialPrice=Double.parseDouble(temp.get("preferentialPrice").toString());
							
							temp.put("preferentialPrice", activityPrice*preferentialPrice/100.00);
						}else{
							temp.put("preferentialPrice", Double.parseDouble(activityGoodsMap.get("preferentialPrice").toString()));
						}
						//活动库存
						Integer stock = (Integer)  activityGoodsMap.get("stock");
						
						//判断库存
						if(stock>0){
							if(goodsBuy-customerGoodsBuy<=0){
								temp.put("stock", 0);
							}else{
								if(stock>goodsBuy-customerGoodsBuy){
									temp.put("stock", goodsBuy-customerGoodsBuy);
								}else{
									temp.put("stock", stock);
								}
								
							}
							
						}else{
							temp.put("stock", 0);
						}
					}
				}
				
				Map map2=new HashMap();
				map2.put("normsInfo", temp);
				map.put(Constant.STATUS, Constant.STATUS_SUCCESS);
				map.put(Constant.DATA, map2);	
			}else{
				map.put(Constant.STATUS, Constant.STATUS_ERROR);
				map.put(Constant.ERROR_CODE, Constant.SYSTEM_NO_DATA);
				return map;
			}
			
		}catch(DaoException de){
			throw new ServiceException(de);
		}
	
		return map;
	}


	
	/** 
	  * @package com.sayimo.school.mall.module.goods.dao
	  * @author chenpy
	  * @Title:  
	  * @Description: 
	  * @param 
	  * @throws 
	  * @return Map
	  * @date 2016年7月11日
	  */ 
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Map getNormsValueInfoForShoppingCart(Integer goodsId, Integer goodsNormsValueId) throws DaoException{
		Map paramMap = new HashMap();
		paramMap.put("goodsId", goodsId);
		paramMap.put("goodsNormsValueId", goodsNormsValueId);
		return ((Map)this.goodsNormsInfoInterfaceDao.getNormsValueInfoForShoppingCart(paramMap));
	}
}
